﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.CommandUIDefinitionClasses
{
    public class Controls : ICommandUIDefinition, IParentable, IXmlBuildable, IChildDeletable
    {
        public bool IsChildOfCommandUIDefinition { get; set; }

        public string Id { get; set; }
        public string ToolTipDescription { get; set; }
        public string ToolTipHelpKeyWord { get; set; }
        public string ToolTipImage32by32 { get; set; }
        public string ToolTipImage32by32Class { get; set; }
        public int? ToolTipImage32by32Left { get; set; }
        public int? ToolTipImage32by32Top { get; set; }
        public string ToolTipShortcutKey { get; set; }
        public string ToolTipTitle { get; set; }
        public List<IControlsChild> ChildControls { get; set; }

        public Controls(bool isFromCommandUIDefinition)
        {
            this.IsChildOfCommandUIDefinition = isFromCommandUIDefinition;
            this.ChildControls = new List<IControlsChild>();
        }

        public Controls(XmlNode node, bool isFromCommandUIDefinition)
            : this(isFromCommandUIDefinition)
        {
            try
            {
                this.Id = XmlHelper.GetAttribute(node, "Id");
                this.ToolTipDescription = XmlHelper.GetAttribute(node, "ToolTipDescription");
                this.ToolTipHelpKeyWord = XmlHelper.GetAttribute(node, "ToolTipHelpKeyWord");
                this.ToolTipImage32by32 = XmlHelper.GetAttribute(node, "ToolTipImage32by32");
                this.ToolTipImage32by32Class = XmlHelper.GetAttribute(node, "ToolTipImage32by32Class");
                this.ToolTipImage32by32Left = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Left");
                this.ToolTipImage32by32Top = XmlHelper.GetNullableIntAttribute(node, "ToolTipImage32by32Top");
                this.ToolTipShortcutKey = XmlHelper.GetAttribute(node, "ToolTipShortcutKey");
                this.ToolTipTitle = XmlHelper.GetAttribute(node, "ToolTipTitle");

                this.ChildControls = new List<IControlsChild>();

                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Comment)
                        return;

                    if (!isFromCommandUIDefinition && childNode.Name != "ToggleButton" && childNode.Name != "FlyoutAnchor" && childNode.Name != "Button")
                    {
                        // Do nothing
                    }
                    else
                    {
                        IControlsChild icChild = UIDefinitionHelper.GetControlsChildFromXml(childNode);
                        this.ChildControls.Add(icChild);
                    }

                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void AddNode(XmlNode commandUINode)
        {
            try
            {
                XmlNode controlsNode = XmlHelper.AddNode(commandUINode, "Controls");

                XmlHelper.AddAttribute(controlsNode, "Id", this.Id);
                XmlHelper.AddAttribute(controlsNode, "ToolTipDescription", this.ToolTipDescription);
                XmlHelper.AddAttribute(controlsNode, "ToolTipHelpKeyWord", this.ToolTipHelpKeyWord);
                XmlHelper.AddAttribute(controlsNode, "ToolTipImage32by32", this.ToolTipImage32by32);
                XmlHelper.AddAttribute(controlsNode, "ToolTipImage32by32Class", this.ToolTipImage32by32Class);
                XmlHelper.AddAttribute(controlsNode, "ToolTipImage32by32Left", this.ToolTipImage32by32Left);
                XmlHelper.AddAttribute(controlsNode, "ToolTipImage32by32Top", this.ToolTipImage32by32Top);
                XmlHelper.AddAttribute(controlsNode, "ToolTipShortcutKey", this.ToolTipShortcutKey);
                XmlHelper.AddAttribute(controlsNode, "ToolTipTitle", this.ToolTipTitle);

                foreach (IControlsChild icChild in this.ChildControls)
                {
                    icChild.AddNode(controlsNode);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode node = new TreeNode("Controls");
                node.Tag = this;

                string[] childControls = null;

                if (parentNode.Text == "CommandUIDefinition")
                {
                    childControls = new string[] { "Button", "CheckBox", "ComboBox", "DropDown", "FlyoutAnchor", "GalleryButton", "Label", "MRUSplitButton", "Spinner", "SplitButton", "TextBox", "ToggleButton" };
                    node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, childControls, node.Parent.Text != "Group");
                }
                else if (parentNode.Text == "MenuSection")
                {
                    childControls = new string[] { "Button", "ColorPicker", "FlyoutAnchor", "InsertTable", "ToggleButton" };
                    node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, childControls, true);
                }
                else
                {
                    childControls = new string[] { "Button", "FlyoutAnchor", "ToggleButton" };
                    node.ContextMenuStrip = ContextMenuStripHelper.BuildUICommandDefContextMenuStrip(node, childControls, true);
                }

                if (this.ChildControls != null)
                {
                    foreach (IControlsChild icChild in this.ChildControls)
                    {
                        icChild.BuildTreeViewElement(node);
                    }
                }

                parentNode.Nodes.Add(node);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IParentable Members

        public void AddChild(ICommandUIDefinition child)
        {
            try
            {
                if (!(child is IControlsChild))
                {
                    throw new Exception("Unsupported child type: " + child.GetType().ToString());
                }
                else
                {
                    if (this.ChildControls == null)
                    {
                        this.ChildControls = new List<IControlsChild>();
                    }

                    this.ChildControls.Add((IControlsChild)child);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion

        #region IChildDeletable Members

        public void DeleteChild(object o)
        {
            try
            {
                if (o is IControlsChild)
                {
                    this.ChildControls.Remove((IControlsChild)o);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #endregion
    }
}
